(ns ken.pu.main.try-query
  (:use [ken.pu.lucene-helper]
        [ken.pu.mycampus-helper]
        [clojure.contrib.str-utils])
  (:import (java.io BufferedReader)))

(def *analyzer* (mk-analyzer))

(defn str-val [v]
  (format "VALUE://%s:%s [%s]" (:entity v) (:attr v) (:value v)))

(defn str-ent [ent]
  (str-join "\n"
    (cons (format "ENTITY://%s:%s" (:entity ent) (:id ent))
          (for [[k v] ent :when (not (or (= k :entity) (= k :id)))]
            (format "%s = %s" k v)))))

(defn- search-everything 
  "Searches in all indexes for the query string qs"
  [qs]
  (do
    (let [src (mk-index-searcher "mycampus-values.idx")
          q (ngram-query qs)]
      (doseq [val (index-search src q 5)]
        (-> val str-val println))
      (.close src))
    (let [src (mk-index-searcher "mycampus-entities.idx")
          q (mk-simple-query :and *analyzer* qs)]
      (doseq [ent (index-search src q 5)]
        (-> ent str-ent print))
      (println)
      (.close src))))
  
(search-everything "k p")
(search-everything "csc 3030")
(search-everything "csci 3030u")